// -------------------------------------------------------------
// api: respool.area.server.list
// time: 2020-07-24
// author: minner
// -------------------------------------------------------------
exports['respool.area.server.list'] = obj_api = {
    '1.0': function (args, session, callback) {
        if (!session.account_id) {
            return callback(12, null, '会话已失效');
        }

        // 定义查询条件和排序条件的sql子句（数组）
        let sql_where = [], sql_order = [], sql_limit;

        // 如果有资源池条件
        if (args.respool_id && typeof args.respool_id == 'number') {
            sql_where.push("respool_id = " + args.respool_id);
        }
        // 如果有可用区条件
        if (args.respool_area_id && typeof args.respool_area_id == 'number') {
            sql_where.push("respool_area_id = " + args.respool_area_id);
        }
        // 如果有关键字条件
        if (args.keywords) {
            sql_where.push("CONCAT( IFNULL(server_name, ''), IFNULL(description, '')) LIKE " + mysql.escape('%' + args.keywords + '%'));
        }

        // 如果有排序条件
        if (args.order) {
            args.order = args.order.split(';');
            let accept_order_fields = ['create_time'];
            for (var tmp_i in args.order) {
                let tmp_order = args.order[tmp_i].split(',');
                sql_order.push(tmp_order[0] + (tmp_order[1] ? ' DESC' : ''));
            };
        }


        var page = args.page || 1;
        var limit = args.limit || uim.params.query.DEFAULT_LIMIT;
        if (limit > uim.params.query.MAX_LIMIT) {
            return callback(11, null, '超过限定查询条数');
        }
        //
        sql_where = sql_where.length ? " WHERE " + sql_where.join(' AND ') : '';
        sql_order = sql_order.length ? " ORDER BY " + sql_order.join(', ') : '';
        sql_limit = " LIMIT " + ((page - 1) * limit) + ',' + limit;

        var response = {
            // respool_area_server_list,
            page_info: {
                // total,
                page,
                limit
            }
        };
        uim.db_query3({
            reqs: [
                {
                    sql: "SELECT SQL_CALC_FOUND_ROWS * FROM deploy_respool_area_server" + sql_where + sql_order + sql_limit,
                    done: function ({ err, results, fields, start, end }) {
                        response.respool_area_server_list = results;
                    }
                },
                {
                    sql: "SELECT FOUND_ROWS() c",
                    done: function ({ err, results, fields, start, end }) {
                        response.page_info.total = results[0].c;
                    }
                }
            ],
            callback: function (err, successCount, errorCount, totalTime) {
                if (err) {
                    return callback(1, null, '查询数据库出错' + err)
                }
                if (response.page_info.total && args.ext_info) {
                    let batch_query = {
                        respool_area_info: {
                            table: 'deploy_respool_area',
                            pkey: 'respool_area_id',
                            ids: uim.array_column(response.respool_area_server_list, 'respool_area_id'),
                            fields: 'respool_area_name'
                        },
                        system_info: {
                            table: 'deploy_respool_system',
                            pkey: 'respool_system_id',
                            ids: uim.array_column(response.respool_area_server_list, 'system_id'),
                            fields: 'system_name'
                        },
                        creator_info: {
                            table: 'deploy_admin',
                            pkey: 'admin_id',
                            ids: uim.array_column(response.respool_area_server_list, 'creator'),
                            fields: 'real_name,head_portrait'
                        },
                    };
                    uim.commons.queryRecordsInfoBatch(batch_query, function (objs) {
                        Object.assign(response, objs);
                        callback(0, response);
                    });

                } else {
                    callback(0, response);
                }
            }
        });
    },

};
